var ReferenceManager = new function() {
	this.addReference = function(baseUri, baseXpath, targetUri, targetXpath, onSuccess) {
		Ext.Ajax.request({
			loadMask: true,
			url: 'references',
			params: {
				'action': 'addReferenceWeb2Web',
				'baseUri': baseUri,
				'baseXpath': baseXpath,
				'targetUri': targetUri,
				'targetXpath': targetXpath,
			},
			success: function(resp) {
				var result = Ext.decode(resp.responseText);
				if (onSuccess) {
					onSuccess(result.success);
				}
			}
		});
	},
	
	this.discardQueuedReference = function(uri, xpath, onSuccess) {
		Ext.Ajax.request({
			loadMask: true,
			url: 'references',
			params: {
				'action': 'discardQueuedReference',
				'uri': uri,
				'xpath': xpath
			},
			success: function(resp) {
				var result = Ext.decode(resp.responseText);
				if (onSuccess) {
					onSuccess(result.success);
				}
			}
		});
	},
	
	this.getReferences = function(baseUri, onSuccess) {
		Ext.Ajax.request({
			loadMask: true,
			url: 'references',
			params: {
				'action': 'getReferences',
				'baseUri': baseUri
			},
			success: function(resp) {
				var result = Ext.decode(resp.responseText);
				if (onSuccess) {
					onSuccess(result.rows);
				}
			}
		});
	},
	
	this.getUserList = function(onSuccess) {
		Ext.Ajax.request({
			loadMask: true,
			url: 'references',
			params: {
				'action': 'getUserList',
				'baseUri': baseUri
			},
			success: function(resp) {
				var result = Ext.decode(resp.responseText);
				if (onSuccess) {
					onSuccess(result.rows);
				}
			}
		});
	},
	
	this.setRating = function(uri, rating, onSuccess) {
		Ext.Ajax.request({
			loadMask: true,
			url: 'references',
			params: {
				'action': 'setRating',
				'uri': uri,
				'rating': rating
			},
			success: function(resp) {
				var result = Ext.decode(resp.responseText);
				if (onSuccess) {
					onSuccess(result.success);
				}
			}
		});
	},
	
	this.addSemanticRelation = function(refId, semanticRelation, onSuccess){
		Ext.Ajax.request({
			loadMask: true,
			url: 'references',
			params: {
				'action': 'setSemanticRelation',
				'refId': refId,
				'semanticRelation': semanticRelation
			},
			success: function(resp) {
				var result = Ext.decode(resp.responseText);
				if (onSuccess) {
					onSuccess(result.success);
				}
			}
		});
	},
	
	this.shareReference = function(refId, username, onSuccess) {
		Ext.Ajax.request({
			loadMask: true,
			url: 'references',
			params: {
				'action': 'shareReference',
				'refId': refId,
				'username': username
			},
			success: function(resp) {
				var result = Ext.decode(resp.responseText);
				if (onSuccess) {
					onSuccess(result.success);
				}
			}
		});
	},
	
	this.getShareRequests = function(onSuccess) {
		Ext.Ajax.request({
			loadMask: true,
			url: 'references',
			params: {
				'action': 'getShareRequests'
			},
			success: function(resp) {
				var result = Ext.decode(resp.responseText);
				if (onSuccess) {
					onSuccess(result.rows);
				}
			}
		});
	},
	
	this.respondToShareRequests = function(shareRequestId, accept, onSuccess) {
		Ext.Ajax.request({
			loadMask: true,
			url: 'references',
			params: {
				'action': 'respondToShareRequests',
				'shareRequestId': shareRequestId,
				'accept': accept
			},
			success: function(resp) {
				var result = Ext.decode(resp.responseText);
				if (onSuccess) {
					onSuccess(result.success);
				}
			}
		});
	},
	
	this.addNote = function(uri, xpath, text, onSuccess) {
		Ext.Ajax.request({
			loadMask: true,
			url: 'references',
			params: {
				'action': 'addNote',
				'uri': uri,
				'xpath': xpath,
				'text': text
			},
			success: function(resp) {
				var result = Ext.decode(resp.responseText);
				if (onSuccess) {
					onSuccess(result.success);
				}
			}
		});
	},
	
	this.getNotes = function(uri, onSuccess) {
		Ext.Ajax.request({
			loadMask: true,
			url: 'references',
			params: {
				'action': 'getNotes',
				'uri': uri
			},
			success: function(resp) {
				var result = Ext.decode(resp.responseText);
				if (onSuccess) {
					onSuccess(result.rows);
				}
			}
		});
	},
	
	this.deleteAnnotation = function(id, onSuccess) {
		Ext.Ajax.request({
			loadMask: true,
			url: 'references',
			params: {
				'action': 'deleteAnnotation',
				'id': id
			},
			success: function(resp) {
				var result = Ext.decode(resp.responseText);
				if (onSuccess) {
					onSuccess(result.success);
				}
			}
		});
	},
	
	this.addTag = function(uri, xpath, tag,win, onSuccess) {
		ReferenceManager.getTags(uri, function(success) {
			var exist = false;
    		for(var i = success.length - 1; i >= 0; --i) {
    			  var o = success[i];
    			  var existingTag = o.tag;
    			  if (tag == existingTag)
    				  exist = true;
    		}		
    		if (exist==false){
    			TagManager.addTagItem(uri, xpath, tag,win);
    			Ext.Ajax.request({
    				loadMask: true,
    				url: 'references',
    				params: {
    					'action': 'addTag',
    					'uri': uri,
    					'xpath': xpath,
    					'tag': tag
    				},
    				success: function(resp) {
    					var result = Ext.decode(resp.responseText);
    					if (onSuccess) {
    						onSuccess(result.success);
    					}
    				}
    			});
    		}
    			
    	});
		
	},	
	this.getTags = function(uri, onSuccess) {
		Ext.Ajax.request({
			loadMask: true,
			url: 'references',
			params: {
				'action': 'getTags',
				'uri': uri
			},
			success: function(resp) {
				var result = Ext.decode(resp.responseText);
				if (onSuccess) {
					onSuccess(result.rows);
				}
			}
		});
	},
	
	this.removeTag = function(uri, xpath, tag, onSuccess) {
		Ext.Ajax.request({
			loadMask: true,
			url: 'references',
			params: {
				'action': 'removeTag',
				'uri': uri,
				'xpath': xpath,
				'tag': tag
			},
			success: function(resp) {
				var result = Ext.decode(resp.responseText);
				if (onSuccess) {
					onSuccess(result.success);
				}
			}
		});
	}
};
